package com.xidian.zhiancloud.service.emergencyManagement;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xidian.zhiancloud.dao.*;
import com.xidian.zhiancloud.dao.syset.SysPositionMapper;
import com.xidian.zhiancloud.dao.syset.SysUserMapper;
import com.xidian.zhiancloud.dao.syset.usermanage.OrganizationMapper;
import com.xidian.zhiancloud.dto.emergencyManagement.EmergencyMaterialsInventoryManagementDTO;

import com.xidian.zhiancloud.dto.emergencyManagement.EmergencyTeamManagementDTO;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.exception.CustomizeException;
import com.xidian.zhiancloud.model.*;
import com.xidian.zhiancloud.model.syset.Organization;
import com.xidian.zhiancloud.model.syset.SysPosition;
import com.xidian.zhiancloud.model.syset.SysUser;
import com.xidian.zhiancloud.qo.emergencyManagement.EmergencyMaterialsInventoryManagementQO;
import com.xidian.zhiancloud.vo.emergencyManagement.*;
import com.xidian.zhiancloud.vo.ledgerManagerment.relatedParty.RelatedPartyBlackListVO;
import com.xidian.zhiancloud.vo.ledgerManagerment.targetAndSystem.SecurityCulturalActivityLedgerVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class EmergencyMaterialsInventoryManagementService extends ServiceImpl<EmergencyMaterialsInventoryManagementMapper, EmergencyMaterialsInventoryManagement> implements IService<EmergencyMaterialsInventoryManagement> {

    @Autowired
    private EmergencyMaterialsInventoryManagementMapper emergencyMaterialsInventoryManagementMapper;

    @Autowired
    private WarehousingMapper warehousingMapper;

    @Autowired
    private WarehouseOutMapper warehouseOutMapper;

    @Autowired
    private OrganizationMapper organizationMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysPositionMapper sysPositionMapper;

    @Transactional(rollbackFor = {RuntimeException.class, Error.class, CustomizeException.class})
    public Boolean add(EmergencyMaterialsInventoryManagement emergencyMaterialsInventoryManagement) {
        int result = emergencyMaterialsInventoryManagementMapper.insert(emergencyMaterialsInventoryManagement);
        if (result == 0) {
            throw new CustomizeException(GeneralResponseEnums.SYSTEM_ERROR);
        }
        return true;
    }

    public EmergencyMaterialsInventoryManagementVO getById(Long id){
        EmergencyMaterialsInventoryManagement emergencyMaterialsInventoryManagement = emergencyMaterialsInventoryManagementMapper.selectById(id);
        if (emergencyMaterialsInventoryManagement == null) {
            throw new CustomizeException(GeneralResponseEnums.GET_FAILED);
        }
        EmergencyMaterialsInventoryManagementVO vo = new EmergencyMaterialsInventoryManagementVO();
        BeanUtils.copyProperties(emergencyMaterialsInventoryManagement, vo);
        SysPosition local= sysPositionMapper.selectById(vo.getLocaleOrganizationId());
        vo.setOrganizationLocale(local==null?"该位置不存在":local.getName());
        Organization org = organizationMapper.selectById(vo.getOrganizationId());
        vo.setOrganizationName(org==null?"该部门不存在":org.getName());
        SysUser user = sysUserMapper.selectById(vo.getLiableP());
        vo.setLiablePersonnel(user == null ? "该用户已被删除" : user.getName());
        return vo;
    }

//    public EmergencyMaterialsInventoryManagementDTO getById(Long id) {
//        EmergencyMaterialsInventoryManagement emergencyMaterialsInventoryManagement = emergencyMaterialsInventoryManagementMapper.selectById(id);
//        if(emergencyMaterialsInventoryManagement==null){
//            throw new CustomizeException(GeneralResponseEnums.SYSTEM_ERROR);
//        }
//        //修改为mybatis plus的条件查询
//        QueryWrapper<Warehousing> warehousingQueryWrapper=new QueryWrapper<Warehousing>();
//        warehousingQueryWrapper.eq("emergency_materials_id",id);
//        List<Warehousing> warehousingList=warehousingMapper.selectList(warehousingQueryWrapper);
//        QueryWrapper<WarehouseOut> warehouseOutQueryWrapper=new QueryWrapper<WarehouseOut>();
//        warehouseOutQueryWrapper.eq("emergency_materials_id",id);
//        List<WarehouseOut> warehouseOutList=warehouseOutMapper.selectList(warehouseOutQueryWrapper);
////        WarehousingExample warehousingExample = new WarehousingExample();
////        WarehouseOutExample warehouseOutExample = new WarehouseOutExample();
////        warehousingExample.createCriteria().andEmergencyMaterialsIdEqualTo(id);
////        warehouseOutExample.createCriteria().andEmergencyMaterialsIdEqualTo(id);
////        List<Warehousing> warehousingList = warehousingMapper.selectByExample(warehousingExample);
////        List<WarehouseOut> warehouseOutList = warehouseOutMapper.selectByExample(warehouseOutExample);
//        EmergencyMaterialsInventoryManagementDTO emergencyMaterialsInventoryManagementDTO = new EmergencyMaterialsInventoryManagementDTO();
//        emergencyMaterialsInventoryManagementDTO.setEmergencyMaterialsInventoryManagement(emergencyMaterialsInventoryManagement);
//        emergencyMaterialsInventoryManagementDTO.setWarehousingList(warehousingList);
//        emergencyMaterialsInventoryManagementDTO.setWarehouseOutList(warehouseOutList);
//        return emergencyMaterialsInventoryManagementDTO;
//    }

    @Transactional(rollbackFor = {RuntimeException.class, Error.class, CustomizeException.class})
    public Boolean deleteByIds(Long[] ids) {
        int res=0;
        for(int i=0; i<ids.length;i++){
            res+=emergencyMaterialsInventoryManagementMapper.deleteById(ids[i]);
//            //mybatis plus的条件删除
//            LambdaQueryWrapper<Warehousing> warehousingLambdaQueryWrapper=Wrappers.<Warehousing>lambdaQuery();
//            warehousingLambdaQueryWrapper.eq(Warehousing::getEmergencyMaterialsId,ids[i]);
//            warehousingMapper.delete(warehousingLambdaQueryWrapper);
//            LambdaQueryWrapper<WarehouseOut> warehouseOutLambdaQueryWrapper=Wrappers.<WarehouseOut>lambdaQuery();
//            warehouseOutLambdaQueryWrapper.eq(WarehouseOut::getEmergencyMaterialsId,ids[i]);
//            warehouseOutMapper.delete(warehouseOutLambdaQueryWrapper);
        }
        if (res == 0) {
            return false;
        }
        return true;
    }

//    @Transactional(rollbackFor = {RuntimeException.class, Error.class, CustomizeException.class})
//    public Boolean patchById(EmergencyMaterialsInventoryManagementDTO emergencyMaterialsInventoryManagementDTO) {
//        EmergencyMaterialsInventoryManagement emergencyMaterialsInventoryManagement = emergencyMaterialsInventoryManagementDTO.getEmergencyMaterialsInventoryManagement();
//        int result = emergencyMaterialsInventoryManagementMapper.updateById(emergencyMaterialsInventoryManagement);
//        if (result == 0) {
//            throw new CustomizeException(GeneralResponseEnums.SYSTEM_ERROR);
//        }

//        LambdaUpdateWrapper<Warehousing> warehousingLambdaUpdateWrapper=Wrappers.<Warehousing>lambdaUpdate();
//        warehousingLambdaUpdateWrapper.eq(Warehousing::getEmergencyMaterialsId,emergencyMaterialsInventoryManagement.getId());
//        List<Warehousing> warehousingList=emergencyMaterialsInventoryManagementDTO.getWarehousingList();
//        for (Warehousing warehousing : warehousingList) {
//            warehousingMapper.update(warehousing,warehousingLambdaUpdateWrapper);
//        }
//        LambdaUpdateWrapper<WarehouseOut> warehouseOutLambdaUpdateWrapper=Wrappers.<WarehouseOut>lambdaUpdate();
//        warehouseOutLambdaUpdateWrapper.eq(WarehouseOut::getEmergencyMaterialsId,emergencyMaterialsInventoryManagement.getId());
//        List<WarehouseOut> warehouseOutList=emergencyMaterialsInventoryManagementDTO.getWarehouseOutList();
//        for (WarehouseOut warehouseOut : warehouseOutList) {
//            warehouseOutMapper.update(warehouseOut,warehouseOutLambdaUpdateWrapper);
//        }


//        List<Warehousing> list1 = emergencyMaterialsInventoryManagementDTO.getWarehousingList();
//        List<WarehouseOut> list2 = emergencyMaterialsInventoryManagementDTO.getWarehouseOutList();
//        int count1=0;
//        for(Warehousing warehousing : list1){
//            //修改成小表的mp修改方式
//            count1+=warehousingMapper.updateById(warehousing);
//        }
//        if(count1!=list1.size()){
//            throw new CustomizeException(GeneralResponseEnums.SYSTEM_ERROR);
//        }
//        int count2=0;
//        for(WarehouseOut warehouseOut : list2){
//            //修改成小表的mp修改方式
//            count2+=warehouseOutMapper.updateById(warehouseOut);
//        }
//        if(count2!=list2.size()){
//            throw new CustomizeException(GeneralResponseEnums.SYSTEM_ERROR);
//        }
//    }

    public IPage<EmergencyMaterialsInventoryManagementVO> getListByQO(EmergencyMaterialsInventoryManagementQO emergencyMaterialsInventoryManagementQO) {
        Page<EmergencyMaterialsInventoryManagementVO> page = new Page<>(emergencyMaterialsInventoryManagementQO.getCurrent(), emergencyMaterialsInventoryManagementQO.getSize());
        OrderItem orderItem = new OrderItem();
        orderItem.setColumn("create_time").setAsc(false);
        page.getOrders().add(orderItem);
        IPage<EmergencyMaterialsInventoryManagementVO> ans= emergencyMaterialsInventoryManagementMapper.getListByQO(page,emergencyMaterialsInventoryManagementQO);
        for (EmergencyMaterialsInventoryManagementVO record : ans.getRecords()) {
            Organization org = organizationMapper.selectById(record.getOrganizationId());
            record.setOrganizationName(org==null?"该部门不存在":org.getName());
            SysPosition p=sysPositionMapper.selectById(record.getLocaleOrganizationId());
            record.setOrganizationLocale(p==null?"该位置不存在":p.getName());
            SysUser userid =sysUserMapper.selectById(record.getLiableP());
            record.setLiablePersonnel(userid==null?"该用户不存在":userid.getName());
        }
        return ans;
    }




}
